package io.renren.system.log.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.export.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.system.log.dto.SysLogLoginDTO;
import io.renren.system.log.excel.SysLogLoginExcel;
import io.renren.system.log.service.SysLogLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 【日志管理-登录日志】
 */
@RestController
@RequestMapping("sys/log/login")
@Api(tags = "【日志管理-登录日志】")
public class SysLogLoginController {
    @Autowired
    private SysLogLoginService sysLogLoginService;

    @GetMapping("page")
    @ApiOperation("按查询条件分页获取日志列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "creatorName", value = "用户名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "operation", value = "操作类型", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态(0-失败;1-成功;2-账号已锁定)", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "startDate", value = "开始日期", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", paramType = "query", dataType = "String")
    })
    @RequiresPermissions("sys:log:login")
    public Result<PageData<SysLogLoginDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<SysLogLoginDTO> page = sysLogLoginService.page(params);
        return new Result<PageData<SysLogLoginDTO>>().ok(page);
    }

    @GetMapping("export")
    @ApiOperation("按查询条件导出日志列表数据")
    @LogOperation("按查询条件导出日志列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "creatorName", value = "用户名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "operation", value = "操作类型", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态(0-失败;1-成功;2-账号已锁定)", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "startDate", value = "开始日期", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", paramType = "query", dataType = "String")
    })
    @RequiresPermissions("sys:log:login")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SysLogLoginDTO> list = sysLogLoginService.list(params);
        ExcelUtils.exportExcelToTarget(response, "登录日志列表", list, SysLogLoginExcel.class);
    }
}